package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.Medicine;

public class MedicineDAOImpl extends BaseHibernateDAOImpl<Medicine> implements MedicineDAO {

	public Medicine getMedicineByMedicineID(String medicineID) {
		Medicine medicine;
		try {
			medicine = (Medicine) getSession().get(Medicine.class, medicineID);

		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return medicine;
	}

	@SuppressWarnings("unchecked")
	public List<Medicine> getAllMedicineByCondition(String hql) {
		List<Medicine> Medicines;
		try {
			Query query = getSession().createQuery("from Medicine" + hql);
			Medicines = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return Medicines;
	}

	@SuppressWarnings("unchecked")
	public List<Medicine> getAllMedicineByConditionByPage(String hql, int currentPage, int lineSize) {
		List<Medicine> medicines;
		try {
			Query query = getSession().createQuery("from Medicine" + hql);
			query.setFirstResult((currentPage - 1) * lineSize);
			query.setMaxResults(lineSize);
			medicines = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return medicines;
	}

}
